package board.animation;

import java.util.ArrayList;
/** A group of animations who will be animated
 *  in parallel and have a common procedure on
 *  completion.  
 * 
 * @author R.Wisniewski1101
 *
 * @param <T> - the greatest common type of the occupants of the animations.  
 */
public class AnimationGroup<T> extends ArrayList<Animation<T>> {
	//For ArrayList, I think.  Eclipse did it.  
	private static final long serialVersionUID = -7432596936846881306L;
	/** The procedure to run when all of the
	 *  animations have completed.  
	 */
	private Runnable finishTask;
	/** Creates an empty group with no end task
	 *  and a capacity of 5.  
	 */
	public AnimationGroup() {super(5);}
	/** Creates an empty group with no end task
	 *  and a defined initial capacity.  
	 * 
	 * @param capacity - the initial capacity.  
	 */
	public AnimationGroup(int capacity) {super(capacity);}
	/** Creates a group that starts with a single animation. 
	 * 
	 * @param animation - the first animation.  
	 */
	public AnimationGroup(Animation<T> animation) {super(1);add(animation);}
	/** Create an empty group with the given end task.  
	 * 
	 * @param finishTask - the initial end task.  
	 */
	public AnimationGroup(Runnable finishTask) {super(5);setFinishTask(finishTask);}
	/** Create a group with a capacity of 1
	 *  that contains the given animation and
	 *  the given end task.  
	 * 
	 * @param animation - the initial animation
	 * @param finishTask - 
	 */
	public AnimationGroup(Animation<T> animation, Runnable finishTask) {super(1);add(animation); setFinishTask(finishTask);}
	/** Create an empty group with a given
	 *  end task and capacity.  
	 * 
	 * @param finishTask - the end task
	 * @param compacity - the capacity
	 */
	public AnimationGroup(Runnable finishTask, int compacity) {super(compacity); setFinishTask(finishTask);}
	/** Calls next for all of the animations, 
	 *  then removes them if they are complete.  
	 *  Finally, the end task is called if
	 *  there are no more animations in this
	 *  group.  
	 */
	public void next() {
		//if this group is complete, do nothing.  
		if(!isComplete()) {
			//call next for each animation, removing the complete ones
			for(int x = 0; x < size();) {
				get(x).next();
				//if the animation is complete, remove it
				if(get(x).isComplete())
					remove(x);
				//otherwise, move to next animation
				else
					++x;
			}
			//if the group is now complete and there is a complete task, call it.  
			if(isComplete() && finishTask != null)
				finishTask.run();
		}
	}
	/** Are there any animations in the group.  
	 * 
	 * @return - the state of this group.  
	 */
	public boolean isComplete() {
		return size() == 0;
	}
	/** Set the task that will be called when
	 *  all of the animations in this group
	 *  are complete.  
	 * 
	 * @param newTask - the new end task.  
	 */
	public void setFinishTask(Runnable newTask) {finishTask = newTask;}
	/** Get the task that is called when this
	 *  group is complete.  
	 * 
	 * @return - the end task. 
	 */
	public Runnable getFinishTask() {return finishTask;}
}
